<?php

namespace app\pc\controller;

use app\base\controller\Base;
use think\Request;
use think\Cache;
use think\Config;
use think\Session;


class Shares extends Base
{
    public $model;

    public function __construct()
    {
        $this->model = model("shares");
        parent::__construct();
        $this->aliasPost = ["id" => "id", "name" => "user_name", "phone" => "mobile", "ps" => "password", "phone2" => "mobile2"];
        $rows = Cache::get($this->Cstatic->get("sid"));
        $this->sid_user_id = $rows['id'];
    }

    /**
     * 添加股票
     *
     * @return \think\Response
     */
    public function addshare()
    {
        $attribute = $this->attribute;
        //获取数据库配置项的值
        $this->Configs = controller('base/Configs', 'event');
        $res = $this->Configs->get_config_groupid(3);
        $flag = false;
        foreach ($res as $k => $v) {
            if ($v['id'] == $attribute) {
                $flag = true;
                break;
            }
        }
        if ($flag) {
            return resJson(0, $this->ecLang("failInsert3"), $res);
        }
        $code = $this->code;
        $name = $this->name;
        $res = $this->model->field("attribute,code,name")->where(['code' => $code])->find();
        if ($res) {
            return resJson(0, $this->ecLang("failInsert"));
        } else {
            $data = [
                "attribute" => $attribute,
                "code" => $code,
                "name" => $name,

            ];
            $insert_id = $this->model->save($data);
            if ($insert_id) {
                return resJson(1, $this->ecLang("scuessInsert"), $insert_id);
            } else {
                return resJson(0, $this->ecLang("failInsert2"));
            }
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $result = $this->model->field("id")->where(['id' => $id])->find();
        if ($result) {
            $res = $this->model->delete(['id' => $id]);
            if (empty($res)) {
                return failJson($this->ecLang("faildelete"));
            }
            return sucessJson($this->ecLang("scuessdelete"));
        }
        return failJson($this->ecLang("failnodate"));
    }

    /**
     * 模糊搜索股票代码
     * @param  int $code
     * @return \think\Response
     */
    public function search()
    {

        if(isset($this->code)){
            if(preg_match('/[\x{4e00}-\x{9fa5}]/u', $this->code)){
                $where['a.name'] = ['like',"%".$this->code."%"];
            }else{
                $where['a.code'] = ['like',"%".$this->code."%"];
            }

        }
        $res = $this->model->search_page($where);
        return sucessJson($this->ecLang("scuessdelete"), $res);

    }


    /**
     *  获取股票详情
     */
    public function searchget(){
                $id = $this->id;
                $res = $this->model->alias("a")
                    ->field("a.id,configs.key,a.code,configs.value")
                    ->where(['a.id'=>$id])
                ->join("configs","a.attribute = configs.id","left")
                  ->find();

                if(!empty($res)){
                   $rows =  gethqone($res['code'],$res['key']);

                    if(!empty($rows)){

                        $rows['code'] = $res['code'];
                        $rows['value'] = $res['value'];
                    }
                   return sucessJson("查询成功", $rows);
                }else{
                  return failJson("查询失败");

                }


    }

}
